package com.wzt.lianfirecontrol.adapter.xiaokongshi;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.function.xiaokongshi.XiaoKongShiModel;
import com.wzt.lianfirecontrol.bean.recode.function.xiaokongshi.XksRecordModel;

/**
 * 消控室值班
 */
public class DutyListAdapter extends BaseRecyclerAdapter<XiaoKongShiModel> {

    private Context context;

    private boolean isHistory = false;

    public boolean isHistory() {
        return isHistory;
    }

    private DutyCallBack dutyCallBack;

    public void setHistory(boolean history) {
        isHistory = history;
    }

    public DutyListAdapter(Context context, DutyCallBack dutyCallBack) {
        this.context = context;
        this.dutyCallBack = dutyCallBack;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_duty_list, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, XiaoKongShiModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{

        private TextView tv_duty_title;
        private ImageView iv_xks_video;
        private RecyclerView rlv_duty_item_list;
        private DutyListItemAdapter dutyListItemAdapter;

        public MyViewHolder(View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.tv_duty_title = itemView.findViewById(R.id.tv_duty_title);
        myViewHolder.iv_xks_video = itemView.findViewById(R.id.iv_xks_video);
        myViewHolder.rlv_duty_item_list = itemView.findViewById(R.id.rlv_duty_item_list);
        myViewHolder.dutyListItemAdapter = new DutyListItemAdapter(context);
        myViewHolder.dutyListItemAdapter.setHistory(isHistory);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        myViewHolder.rlv_duty_item_list.setLayoutManager(linearLayoutManager);
        myViewHolder.rlv_duty_item_list.setAdapter(myViewHolder.dutyListItemAdapter);
    }

    private void setItemData(MyViewHolder viewHolder, final XiaoKongShiModel data, int realPosition) {
        viewHolder.tv_duty_title.setText(data.getName());
        viewHolder.dutyListItemAdapter.clear();
        if (null != data.getRecords() && data.getRecords().size() > 0) {
            viewHolder.dutyListItemAdapter.addDatas(data.getRecords());
            viewHolder.rlv_duty_item_list.setVisibility(View.VISIBLE);
            viewHolder.dutyListItemAdapter.setOnItemClickListener(new OnItemClickListener<XksRecordModel>() {
                @Override
                public void onItemClick(View v, int position, XksRecordModel XksRecordModel) {
                    dutyCallBack.itemClick(XksRecordModel);
                }
            });
        }else {
            viewHolder.rlv_duty_item_list.setVisibility(View.GONE);
        }
        if (null != data.getVideos() && data.getVideos().size() > 0) {
            viewHolder.iv_xks_video.setVisibility(View.VISIBLE);
            viewHolder.iv_xks_video.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (data.getVideos().size() == 1) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("JianKongDeviceModel", data.getVideos().get(0));
                        ((BaseActivity)context).setClickAction(ConstData.VIDEO, data.getVideos().get(0).getAccessMode(), "监控设备", null, bundle);
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putString(ConstData.SHOWTYPPE, ConstData.XKSVIDEOLIST);
                        bundle.putSerializable("XiaoKongShiModel", data);
                        ((BaseActivity) context).setClickAction(ConstData.SHOWFRAG, ConstData.INFOLIST, "监控设备", null, bundle);
                    }
                }
            });
        }else {
            viewHolder.iv_xks_video.setVisibility(View.GONE);
        }
    }

    public interface DutyCallBack{
        void itemClick(XksRecordModel xksRecordModel);
    }
}

